const express = require('express');
const router = express.Router();
const { formatResponseBody } = require("../utils/tools");
const blogService = require("../service/blogService");

// 分页获取文章
router.get("/", async (req, res, next) => {
    const data = await blogService.pagingGetBlogService(req.query.page, req.query.limit, req.query.keyword, req.query.categroyId);
    res.send(formatResponseBody({ data }))
})

// 获取单篇文章
router.get("/:id", async (req, res, next) => {
    const data = await blogService.getOneBlogService(req.params.id, req.headers);
    res.send(formatResponseBody({ data }));
})

// 添加一篇文章
router.post("/", async (req, res, next) => {
    const data = await blogService.addOneBlogService(req.body);
    res.send(formatResponseBody({ data }))
})

// 删除一个分类
router.delete("/:id", async (req, res, next) => {
    const data = await blogService.deleteOnBlogService(req.params.id);
    res.send(formatResponseBody({ data }));
})

// 修改一个分类
router.put("/:id", async (req, res, next) => {
    const data = await blogService.updateOneBlogService(req.body, req.params.id);
    res.send(formatResponseBody({ data }));
})

module.exports = router;